package cn.xiaopengstack.domain.agent.service.execute.auto.node;

import cn.bugstack.wrench.design.framework.tree.StrategyHandler;
import cn.xiaopengstack.domain.agent.model.entity.ExecuteCommandEntity;
import cn.xiaopengstack.domain.agent.model.valobj.AiAgentClientFlowConfigVO;
import cn.xiaopengstack.domain.agent.service.execute.auto.AbstractExecuteSupport;
import cn.xiaopengstack.domain.agent.service.execute.auto.factory.DefaultAutoAgentExecuteStrategyFactory;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author jiangyangang
 */
@Component("executeRootNode")
@Slf4j
public class RootNode extends AbstractExecuteSupport {

    @Override
    protected String doApply(ExecuteCommandEntity requestParameter, DefaultAutoAgentExecuteStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("=== 动态多轮执行测试开始 ====");
        log.info("用户输入: {}", requestParameter.getMessage());
        log.info("最大执行步数: {}", requestParameter.getMaxSteps());
        log.info("会话ID: {}", requestParameter.getSessionId());
        // 加载数据
        Map<String, AiAgentClientFlowConfigVO> configVOMap = repository.queryAiAgentClientFlowConfig(requestParameter.getAiAgentId());

        dynamicContext.setCompleted(false);
        dynamicContext.setMaxSteps(requestParameter.getMaxSteps());
        dynamicContext.setExecutionHistory(new StringBuilder());
        dynamicContext.setAiAgentClientFlowConfigVOMap(configVOMap);

        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<ExecuteCommandEntity, DefaultAutoAgentExecuteStrategyFactory.DynamicContext, String> get(ExecuteCommandEntity requestParameter, DefaultAutoAgentExecuteStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return getBean("step1AnalyzerNode");
    }
}
